//
//  Muse_ex_imageView.swift
//  Muse
//
//  Created by mac on 2022/12/8.
//

import UIKit
import Kingfisher
import Stevia

class IndicatorView: Indicator {
    
    let view = UIView()
    
    lazy var indicatorView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(style: .medium)
        view.color = "38954D".toColor
        return view
    }()
    
    init() {
        view.subviews(indicatorView)
        indicatorView.centerInContainer().sizeToFit()
    }
    
    func startAnimatingView() {
        view.isHidden = false
        if !indicatorView.isAnimating {
            indicatorView.startAnimating()
        }
    }
    
    func stopAnimatingView() {
        view.isHidden = true
        if indicatorView.isAnimating {
            indicatorView.stopAnimating()
        }
    }
}

extension UIImageView {
    
    func loadImage(url: String?, placehold: String? = "FJ_Common_avator") {
//        self.backgroundColor = "E3E3E3".toColor
        self.kf.indicatorType = .custom(indicator: IndicatorView())
        self.kf.setImage(with: URL(string: url ?? ""), placeholder: UIImage(named: placehold ?? ""), options: [.transition(.fade(0.3)), .processor(DownsamplingImageProcessor(size: UIScreen.main.bounds.size))], completionHandler: nil)
    }
    
    func loadImage(url: String?, placehold: String? = "FJ_Common_avator", blurRadius: Int) {
        self.kf.indicatorType = .activity
        let processor = BlurImageProcessor(blurRadius: CGFloat(blurRadius))
        self.kf.setImage(with: URL(string: url ?? ""), placeholder: UIImage(named: placehold ?? ""), options: [.transition(.fade(0.3)), .processor(processor), .processor(DownsamplingImageProcessor(size: UIScreen.main.bounds.size))], completionHandler: nil)
    }
    
}

extension FJMakeViewWrapper where Base: UIImageView {
    
    @discardableResult
    func image(url: String?, placeholder: String? = nil) -> Self {
        base.loadImage(url: url, placehold: placeholder)
        return self
    }
    
}
